﻿// PeLearn.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <functional>
#include <Windows.h>
#include <ImageHlp.h>
#include "Pe.h"
#include "ShellInject.h"












////添加导入函数(暂未完成)
//void add_import_function(char* imageBase,char* moduleName,char* funName) {
//
//    bool is_import_module = false;
//    bool is_import_fun = false;
//    PIMAGE_DOS_HEADER pDos = (PIMAGE_DOS_HEADER)imageBase;
//    PIMAGE_NT_HEADERS pNts = (PIMAGE_NT_HEADERS)(imageBase + pDos->e_lfanew);
//    IMAGE_DATA_DIRECTORY imports = pNts->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
//    PIMAGE_IMPORT_DESCRIPTOR import = (PIMAGE_IMPORT_DESCRIPTOR)(imports.VirtualAddress + imageBase);
//    do {
//
//
//        for (; import->OriginalFirstThunk; import++) {
//
//            char* name = (imageBase + import->Name);
//            if (stricmp(name, moduleName) == 0) {
//                is_import_module = true;
//                PIMAGE_THUNK_DATA intTable = (PIMAGE_THUNK_DATA)(imageBase + import->OriginalFirstThunk);
//                PIMAGE_THUNK_DATA iatTable = (PIMAGE_THUNK_DATA)(imageBase + import->FirstThunk);
//                for (; intTable->u1.AddressOfData; intTable++) {
//                    PIMAGE_IMPORT_BY_NAME funOrder = (PIMAGE_IMPORT_BY_NAME)(imageBase + intTable->u1.AddressOfData);
//                    int order = funOrder->Hint;
//                    name = (char*)funOrder->Name;
//                    if (stricmp(name, funName) == 0) {
//                        is_import_fun = true;
//                        return;
//                    }
//                }
//                break;
//           }
//
//        }
//
//
//    } while (0);
//    if (is_import_module) {
//        HMODULE hmodule = GetModuleHandleA(moduleName);
//        PVOID address  =    GetProcAddress(hmodule, funName);
//   }else{
//        
//    
//    
//    }
//
//}










////添加dll 文件 到 目标文件，并且合并 区段 导入表  重定位表
//void add_dll_to_image(char** imageBase,const char* path) {
//
//    char* dll_buffer  = map_filememory_to_virtualmemory(path);
//    if (!dll_buffer) {
//        return;
//    }
//
//    PIMAGE_DOS_HEADER pdll_Dos = (PIMAGE_DOS_HEADER)dll_buffer;
//    PIMAGE_NT_HEADERS pdll_Nts = (PIMAGE_NT_HEADERS)(dll_buffer + pdll_Dos->e_lfanew);
//    PIMAGE_DOS_HEADER pexe_Dos = (PIMAGE_DOS_HEADER)*imageBase;
//    PIMAGE_NT_HEADERS pexe_Nts = (PIMAGE_NT_HEADERS)(*imageBase + pexe_Dos->e_lfanew);
//    size_t dll_image_base = pdll_Nts->OptionalHeader.ImageBase;
//    size_t exe_image_base = pexe_Nts->OptionalHeader.ImageBase;
//    pdll_Nts->OptionalHeader.ImageBase = (size_t)*imageBase;
//    size_t dll_image_size = pdll_Nts->OptionalHeader.SizeOfImage;
//    
//    //解析dll，添加节区到exe内存
//    PIMAGE_SECTION_HEADER dll_section = IMAGE_FIRST_SECTION(pdll_Nts);
//    char* dll_buffer_temp = (char*)(add_section(imageBase, ".dhea", 0x1000, dll_buffer) - *imageBase);
//
//
//    for (; dll_section->VirtualAddress; dll_section++) {
//        add_section(imageBase, (char*)dll_section->Name, dll_section->SizeOfRawData, dll_section->VirtualAddress + dll_buffer);
//    }
//    char* dll_reloction_section = (char*)(add_section(imageBase, ".drel", 0x1000) - *imageBase);
//    PIMAGE_IMPORT_DESCRIPTOR importAddress = (PIMAGE_IMPORT_DESCRIPTOR)add_section(imageBase, ".impo", 0x1000);
//    size_t origin_import_address = (size_t)importAddress;
//    free(dll_buffer);
//    dll_buffer = dll_buffer_temp + (size_t)*imageBase;
//    size_t origin_relocation_address = (size_t)(dll_reloction_section + (size_t) * imageBase);
//    dll_reloction_section = (char*)origin_relocation_address;
//    pdll_Dos = (PIMAGE_DOS_HEADER)dll_buffer;
//    pdll_Nts = (PIMAGE_NT_HEADERS)(dll_buffer + pdll_Dos->e_lfanew);
//    pexe_Dos = (PIMAGE_DOS_HEADER)*imageBase;
//    pexe_Nts = (PIMAGE_NT_HEADERS)(*imageBase + pexe_Dos->e_lfanew);
//    //解析dll,修复导入表
//    IMAGE_DATA_DIRECTORY pdll_imports =  pdll_Nts->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
//    IMAGE_DATA_DIRECTORY pexe_imports = pexe_Nts->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
//   
//  
//
//
//    if (pexe_imports.VirtualAddress) {
//        PIMAGE_IMPORT_DESCRIPTOR pexe_import = (PIMAGE_IMPORT_DESCRIPTOR)(pexe_imports.VirtualAddress + *imageBase);
//        for (; pexe_import->OriginalFirstThunk; pexe_import++, importAddress++) {
//            memcpy(importAddress, pexe_import, sizeof(IMAGE_IMPORT_DESCRIPTOR));
//        }
//    }
//
//    if (pdll_imports.VirtualAddress) {
//        PIMAGE_IMPORT_DESCRIPTOR pdLL_import = (PIMAGE_IMPORT_DESCRIPTOR)(pdll_imports.VirtualAddress + dll_buffer);
//        for (; pdLL_import->OriginalFirstThunk; importAddress++, pdLL_import++) {
//
//            PIMAGE_THUNK_DATA intTable = (PIMAGE_THUNK_DATA)(dll_buffer + pdLL_import->OriginalFirstThunk);
//            PIMAGE_THUNK_DATA iatTable = (PIMAGE_THUNK_DATA)(dll_buffer + pdLL_import->FirstThunk);
//            for (; intTable->u1.AddressOfData; intTable++) {
//
//                intTable->u1.AddressOfData = intTable->u1.AddressOfData + (size_t)dll_buffer - (size_t)*imageBase;
//                iatTable->u1.AddressOfData = iatTable->u1.AddressOfData + (size_t)dll_buffer - (size_t)*imageBase;
//
//            }
//            pdLL_import->Name = pdLL_import->Name + (size_t)dll_buffer - (size_t)*imageBase;
//            pdLL_import->OriginalFirstThunk = pdLL_import->OriginalFirstThunk + (size_t)dll_buffer - (size_t)*imageBase;
//            pdLL_import->FirstThunk = pdLL_import->FirstThunk + (size_t)dll_buffer - (size_t)*imageBase;
//            memcpy(importAddress, pdLL_import, sizeof(IMAGE_IMPORT_DESCRIPTOR));
//        }
//    }
//
//
//  
//
//   
//     
//     pexe_Nts->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress = (size_t)origin_import_address - (size_t) * imageBase;
//
//
//     //解析dll,修复重定位表
//
//     IMAGE_DATA_DIRECTORY pdll_exports =  pdll_Nts->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];
//     IMAGE_DATA_DIRECTORY pexe_exports = pexe_Nts->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];
//    
//     if (pexe_exports.VirtualAddress) {
//         PIMAGE_BASE_RELOCATION pexe_export = (PIMAGE_BASE_RELOCATION)(pexe_exports.VirtualAddress + *imageBase);
//         for (; pexe_export->VirtualAddress;) {
//             memcpy(dll_reloction_section, pexe_export, pexe_export->SizeOfBlock);
//             dll_reloction_section += pexe_export->SizeOfBlock;
//             pexe_export = (PIMAGE_BASE_RELOCATION)(char*)pexe_export + pexe_export->SizeOfBlock;
//            
//         }
//     }
//        
//     if (pdll_exports.VirtualAddress) {
//         PIMAGE_BASE_RELOCATION pdll_export = (PIMAGE_BASE_RELOCATION)(pdll_exports.VirtualAddress + dll_buffer);
//         for (; pdll_export->VirtualAddress;) {
//             
//
//             short* item = (short*)((char*)pdll_export + sizeof(IMAGE_BASE_RELOCATION));
//             for (; *item; item++) {
//
// 
//                         *(int*)((*item & 0xfff) + pdll_export->VirtualAddress + dll_buffer) = (int)(*(int*)((*item & 0xf) + pdll_export->VirtualAddress + dll_buffer) + (size_t)exe_image_base - (size_t)dll_image_base);
//             }
//
//
//
//             pdll_export->VirtualAddress = pdll_export->VirtualAddress + (size_t)dll_buffer - (size_t)*imageBase;
//             memcpy(dll_reloction_section, pdll_export, pdll_export->SizeOfBlock);
//             dll_reloction_section = dll_reloction_section + pdll_export->SizeOfBlock;
//             pdll_export = (PIMAGE_BASE_RELOCATION)((char*)pdll_export + pdll_export->SizeOfBlock);
//            
//         }
//
//     }
// 
//     pexe_Nts->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress = origin_relocation_address -(size_t)*imageBase;
//     pexe_Nts->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size = 0x1000;
//
//}




int main(char* args,char* argv)
{
   
    Inject ij("D:\\RadASM\\Masm\\Projects\\testAsm4.1\\PeLearn - 副本.exe", "D:\\RadASM\\Masm\\Projects\\testAsm4.1\\PeLearn - 副本.exe");

    ij.InjectPe();
    //Pe p("D:\\RadASM\\Masm\\Projects\\testAsm4.1\\testDll.dll");
    //p.run();
   //p.display_pe_sections();
   //p.add_section(".test", 0x1000, NULL);
   //p.dump_imageBase("D:\\RadASM\\Masm\\Projects\\testAsm4.1\\testAsm4.1.2_dump.exe");
   
    //test("D:\\RadASM\\Masm\\Projects\\testAsm4.1\\testAsm4.1.2.exe");
  
}

